// pages/goodsShare/goodsShare.js
const service = require('./goodsShareService.js');
const utilService = require('../../utils/utilService.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    goodsId: null,
    goodsType: 0,
    goodUrl: '',
    goodContent: '',
    image: null,
    pageType: '',
    videoUrl: '',
    show: false,
    options: null,
    shareMsg: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    this.setData({
      options: options
    })
    if (options.good) {
      this.setData({
        good: options.good
      });
    }
    if (options.pageType) {
      this.setData({
        pageType: options.pageType
      });
    }else {
      options.pageType = 3
      this.setData({
        pageType: 3
      })
    }
    this.setData({
      userInfo: getApp().globalData.userInfo
    })
    // 商品详情分享
    if (options.good && options.pageType == '0' && this.data.userInfo.avatarUrl) {
      that.getShareMessage(6);

      console.log('afafafafafafaf' + decodeURIComponent(options.good));
      let good = JSON.parse(decodeURIComponent(options.good));
      let goodId = good.goodsListEntity.id;
      let goodUrl = good.goodsListEntity.cover;
      let goodContent = good.description;
      let goodType = good.goodsListEntity.saleTypeId;
      // 跳转路径
      let path = '/goods/pages/goodsDetails/goodsDetails?goods_id=' + goodId + '&goods_type=' + goodType;
      service.getGoodShare(goodUrl, this.data.userInfo.avatarUrl, path, goodContent,
        function (data) {
          if (data) {
            that.setData({
              image: data
            })
          }
        });
    } else if (options.group && options.pageType == '1' && this.data.userInfo.avatarUrl) {
      that.getShareMessage(5);
      // 拼团分享
      let group = JSON.parse(options.group);
      let path = group.goodUrl;
      let headUrl = this.data.userInfo.avatarUrl;
      let goodDetail = decodeURIComponent(group.pagePath);
      let groupCount = '已经拼' + group.groupCount + '团';
      let goodName = group.goodName;
      let goodsTotal = '现在扫码立即免费领' + group.goodsTotal + '件宝贝';
      service.getGroupBuyShare(path, headUrl, goodDetail, groupCount, goodName, goodsTotal, function (data) {
        if (data) {
          that.setData({
            image: data
          });
        }
      });
    } else if (options.pageType == '2') {
      //公众号
      service.getOfficialAccounts(function (data) {
        if (data) {
          that.setData({
            image: data
          });
        }
      });
    } else if (options.pageType == '3') {
      //开心果冻
      if (options.videoUrl) {
        this.setData({
          videoUrl: decodeURIComponent(options.videoUrl)
        })
      }else {
        let videoUrl;
        videoUrl = getApp().globalData.baseUrl + '/resources/ad/happy-jelly.mp4';
        if (getApp().globalData.baseUrl.indexOf("quxian") != -1) {
          videoUrl = "http://resources.zuanliantech.com/ad/happy-jelly.mp4"
        }
        this.setData({
          videoUrl: videoUrl
        })
      }
      this.getShareMessage(15);
      service.getOfficialAccountGuide(function (data) {
        if (data) {
          that.setData({
            image: data,
          });
        }
      });
    } else {
      wx.showToast({
        title: '缺少必要参数',
        duration: 2000,
        icon: 'none'
      })
    }
  },

  imgClick: function (e) {
    if (this.data.image != null) {
      wx.previewImage({
        current: this.data.image,
        urls: [this.data.image] // 需要预览的图片http链接列表
      });
    }
  },

  videoClose: function (e) {
    this.setData({
      show: false
    })
  },

  playVideo: function (e) {
    this.setData( {
      show: true
    })
  },

  getShareMessage: function (type) {
    var self = this;
    utilService.shareSettings(type,
      function (data) {
        console.log("分享数据", data)
        self.setData({
          shareMsg: data.title
        })
      },
      function (error) {

      }
    );
  },

  onShareAppMessage: function (options) {
    var title = this.data.shareMsg;
    var path = "pages/home/home?userId=" + app.globalData.userId
    title = title.replace(/#/, app.globalData.userInfo.nickName)
    // 商品详情分享
    if (this.data.options.good && this.data.options.pageType == '0' && this.data.userInfo.avatarUrl) {
      let good = JSON.parse(decodeURIComponent(this.data.options.good));
      let goodId = good.goodsListEntity.id;
      let goodUrl = good.goodsListEntity.cover;
      let goodType = good.goodsListEntity.saleTypeId;
      title = title.replace(/{}/, good.goodsListEntity.goodsName)

      // 跳转路径
      path = 'goods/pages/goodsDetails/goodsDetails?goods_id=' + goodId + '&goods_type=' + goodType;

    } else if (this.data.options.group && this.data.options.pageType == '1' && this.data.userInfo.avatarUrl) {
      // 拼团分享
      let group = JSON.parse(this.data.options.group);
      path = decodeURIComponent(group.pagePath)
      let goodName = group.goodName;
      title = title.replace(/{}/, goodName)
    } else if (this.data.options.pageType == '3'){
      path = 'pages/goodsShare/goodsShare?pageType=3&videoUrl=' + this.data.videoUrl
    }
    return {
      title: title,
      path: path,
      shareInfo: app.globalData.userInfo,
      success: function (res) {
        wx.showToast({
          title: "转发成功",
          icon: 'success',
          duration: 2000
        })
      },

      fail: function (res) {
        // 转发失败
        console.info("转发失败")
      }
    }
  }

})